package com.zl.tesseract.scanner.app;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * 账号信息存储.
 * @author: zhangyan
 * @date: 2018/8/5
 */
public class AuthPreferences {
    
    private static final String KEY_USER_ACCOUNT = "account";
    private static final String KEY_USER_PASSWORD = "password";
    private static final String KEY_TOKEN = "token";
    private static final String KEY_BAIDU_TOKEN = "baidu_token";

    public static String getUserAccount() {
        return getString(KEY_USER_ACCOUNT);
    }

    public static void saveUserAccount(String account) {
        saveString(KEY_USER_ACCOUNT, account);
    }

    public static String getUserPassword() {
        return getString(KEY_USER_PASSWORD);
    }

    public static void saveUserPassword(String password) {
        saveString(KEY_USER_PASSWORD, password);
    }

    public static String getToken() {
        return getString(KEY_TOKEN);
    }

    public static void saveToken(String token) {
        saveString(KEY_TOKEN, token);
    }

    public static String getBaiduToken() {
        return getString(KEY_BAIDU_TOKEN);
    }

    public static void saveBaiduToken(String baidu_token) {
        saveString(KEY_BAIDU_TOKEN, baidu_token);
    }

    private static void saveString(String key, String value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.apply();
    }

    private static String getString(String key) {
        return getSharedPreferences().getString(key, null);
    }

    private static void saveBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    private static boolean getBoolean(String key) {
        return getSharedPreferences().getBoolean(key, true);
    }

    private static SharedPreferences getSharedPreferences() {
        return MyApplication.sAppContext.getSharedPreferences("myscanner", Context.MODE_PRIVATE);
    }
}
